//
//  NoteTool.m
//  笔记1
//
//  Created by  on 2022/11/6.
//

#import "NoteTool.h"

@implementation NoteTool
#pragma mark - 找笔记
+ (void)find:(NSInteger)stamp isFile:(BOOL)isFile isDelete:(BOOL)isDelete complete:(nullable void(^) (NSMutableArray <Note*>*result))block{
    NSString *year = [NSDate transToDate:@"yyyy" :stamp];
    NSString *month = [NSDate transToDate:@"MM" :stamp];
    NSString* where = [NSString stringWithFormat:@"where %@=%@ and %@=%@ and %@=%@ and %@=%@ and %@=%@ and %@=%@",
                       bg_sqlKey(@"account"),bg_sqlValue(APP.user.account),
                       bg_sqlKey(@"isFile"),bg_sqlValue(@(isFile)),
                       bg_sqlKey(@"year"),bg_sqlValue(year),
                       bg_sqlKey(@"month"),bg_sqlValue(month),
                       bg_sqlKey(@"isDelete"),bg_sqlValue(@(isDelete)),
                       bg_sqlKey(@"fileName"),bg_sqlValue(@"")];
    [self find:where complete:block];
}
#pragma mark - 找文件夹
+ (void)findFile:(NSInteger)stamp isDelete:(BOOL)isDelete complete:(nullable void(^) (NSMutableArray <Note*>*result))block{
    [self find:stamp isFile:YES isDelete:isDelete complete:block];
}
#pragma mark - 找文件夹里面的笔记
+ (void)findFileNote:(Note *)note isDelete:(BOOL)isDelete complete:(nullable void(^) (NSMutableArray <Note*>*result))block{
    dispatch_async(dispatch_get_main_queue(), ^{
        NSString *year = [NSDate transToDate:@"yyyy" :note.stamp];
        NSString *month = [NSDate transToDate:@"MM" :note.stamp];
        NSString* where = [NSString stringWithFormat:@"where %@=%@ and %@=%@ and %@=%@ and %@=%@ and %@=%@ and %@=%@",
                           bg_sqlKey(@"account"),bg_sqlValue(APP.user.account),
                           bg_sqlKey(@"isFile"),bg_sqlValue(@(NO)),
                           bg_sqlKey(@"year"),bg_sqlValue(year),
                           bg_sqlKey(@"month"),bg_sqlValue(month),
                           bg_sqlKey(@"isDelete"),bg_sqlValue(@(isDelete)),
                           bg_sqlKey(@"fileName"),bg_sqlValue(note.name)];
        [self find:where complete:block];
    });
}
#pragma mark - 找到收藏的笔记，和 文件夹
+ (void)findFavorite:(nullable void(^) (NSMutableArray <Note*>*result))block{
    NSString* where = [NSString stringWithFormat:@"where %@=%@ and %@=%@ and %@=%@ and %@=%@",
                       bg_sqlKey(@"account"),bg_sqlValue(APP.user.account),
                       bg_sqlKey(@"favorite"),bg_sqlValue(@(YES)),
                       bg_sqlKey(@"isDelete"),bg_sqlValue(@(NO)),
                       bg_sqlKey(@"fileName"),bg_sqlValue(@"")];
    [self find:where complete:block];
    
}
#pragma mark - 找到移到废纸篓的笔记，和 文件夹
+ (void)findDelete:(nullable void(^) (NSMutableArray <Note*>*result))block{
    NSString* where = [NSString stringWithFormat:@"where %@=%@ and %@=%@ and %@=%@",
                       bg_sqlKey(@"account"),bg_sqlValue(APP.user.account),
                       bg_sqlKey(@"isDelete"),bg_sqlValue(@(YES)),
                       bg_sqlKey(@"fileName"),bg_sqlValue(@"")];
    [self find:where complete:block];
}
#pragma mark - 更新笔记，和文件夹里面的笔记
+ (void)update:(Note *)note complete:(nullable void(^) (BOOL success))block{
    dispatch_group_t group = dispatch_group_create();
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    
    dispatch_group_enter(group);
    dispatch_group_async(group, queue, ^{
        [note bg_saveOrUpdateAsync:^(BOOL isSuccess) {
            dispatch_async(dispatch_get_main_queue(), ^{
                dispatch_group_leave(group);
            });
        }];
    });
    
    if (note.isFile) {
        dispatch_group_enter(group);
        dispatch_group_async(group, queue, ^{
            
            [self findFileNote:note isDelete:NO complete:^(NSMutableArray<Note *> * _Nonnull result) {
                for (Note *model in result) {
                    model.favorite = note.favorite;
                    model.isDelete = note.isDelete;
                }
                if (result == nil || result.count <= 0) {
                    dispatch_group_leave(group);
                }else{
                    [Note bg_saveOrUpdateArrayAsync:result complete:^(BOOL isSuccess) {
                        dispatch_group_leave(group);
                    }];
                }
            }];
        });
    }
    
    dispatch_group_notify(group, dispatch_get_main_queue(), ^{
        [NotifyTool post:notifyNoteUpdate];
        if (block) {
            block(YES);
        }
    });
}
#pragma mark - 搜索笔记
+ (void)search:(NSString *)text complete:(nullable void(^) (NSMutableArray <Note*>*result))block{
    NSString *iscontainer = [NSString stringWithFormat:@" %@=%@ ",bg_sqlKey(@"isFile"),bg_sqlValue(@(NO))];
    NSString *name = [NSString stringWithFormat:@" %@ like '%%%@%%' ",bg_sqlKey(@"name"),text];
    NSString *remark = [NSString stringWithFormat:@" %@ like '%%%@%%' ",bg_sqlKey(@"account"),text];
    NSString *container = [NSString stringWithFormat:@" %@ like '%%%@%%' ",bg_sqlKey(@"content"),text];
    NSString* where = [NSString stringWithFormat:@"where%@and%@or%@or%@",iscontainer,name,remark,container];
    [self find:where complete:block];
}
#pragma mark - 恢复笔记，和文件夹
+ (void)recover:(NSMutableArray<Note*>*)array complete:(nullable void(^) (BOOL success))block{
    NSMutableArray *temp = [NSMutableArray array];
    NSMutableArray *saveFile = [NSMutableArray array];
    
    for (Note *model in array) {
        model.isDelete = NO;
        if (model.isFile) {
            [saveFile addObject:model];
        }
        [temp addObject:model];
    }
    
    dispatch_group_t group = dispatch_group_create();
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    for (Note *model in saveFile) {
        dispatch_group_enter(group);
        dispatch_group_async(group, queue, ^{
            [self findFileNote:model isDelete:YES complete:^(NSMutableArray<Note *> * _Nonnull result) {
                for (Note *n in result) {
                    n.isDelete = NO;
                }
                [temp addObjectsFromArray:result];
                dispatch_group_leave(group);
            }];
        });
    }
    
    dispatch_group_notify(group, dispatch_get_main_queue(), ^{
        [Note bg_saveOrUpdateArrayAsync:temp complete:^(BOOL isSuccess) {
            dispatch_sync(dispatch_get_main_queue(), ^{
                [NotifyTool post:notifyNoteUpdate];
                if (block) {
                    block(isSuccess);
                }
            });
        }];
    });
}

#pragma mark - 删除
+ (void)delete:(NSMutableArray<Note*>*)array complete:(nullable void(^) (BOOL success))block{
    NSMutableArray *temp = [NSMutableArray array];
    NSMutableArray *saveFile = [NSMutableArray array];
    
    for (Note *model in array) {
        if (model.isFile) {
            [saveFile addObject:model];
        }
        model.select = YES;
        [temp addObject:model];
    }
    
    dispatch_group_t group = dispatch_group_create();
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    for (Note *model in saveFile) {
        dispatch_group_enter(group);
        dispatch_group_async(group, queue, ^{
            [self findFileNote:model isDelete:model.isDelete complete:^(NSMutableArray<Note *> * _Nonnull result) {
                for (Note *n in result) {
                    n.select = YES;
                }
                [temp addObjectsFromArray:result];
                dispatch_group_leave(group);
            }];
        });
    }
    
    dispatch_group_notify(group, dispatch_get_main_queue(), ^{
        [Note bg_saveOrUpdateArrayAsync:temp complete:^(BOOL isSuccess) {
            dispatch_async(dispatch_get_main_queue(), ^{
                NSString* where = [NSString stringWithFormat:@"where %@=%@ and %@=%@",bg_sqlKey(@"account"),bg_sqlValue(APP.user.account),bg_sqlKey(@"select"),bg_sqlValue(@(YES))];
                [Note bg_deleteAsync:nil where:where complete:^(BOOL isSuccess) {
                    dispatch_sync(dispatch_get_main_queue(), ^{
                        [NotifyTool post:notifyNoteUpdate];
                        if (block) {
                            block(isSuccess);
                        }
                    });
                }];
            });
        }];
    });
}

#pragma mark - 从文件夹导出
+ (void)moveOutNote:(Note*)note complete:(nullable void(^) (BOOL success))block{
    [self findFileNote:note isDelete:NO complete:^(NSMutableArray<Note *> * _Nonnull result) {
        if (block == nil) {return;}
        if (result == nil || result.count<=0) {
            block(NO);
        }else{
            for (Note *model in result) {
                model.fileName = @"";
            }
            [result bg_saveOrUpdateAsync:^(BOOL isSuccess) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    [NotifyTool post:notifyNoteUpdate];
                    block(isSuccess);
                });
            }];
        }
    }];
}





+ (void)find:(NSString *)where complete:(nullable void(^) (NSMutableArray <Note*>*result))block{
    [Note bg_findAsync:nil where:where complete:^(NSArray * _Nullable array) {
        dispatch_async(dispatch_get_main_queue(), ^{
            if (block) {
                block([NSMutableArray arrayWithArray:array]);
            }
        });
    }];
}
@end
